<?php
class newsletter_admin extends MX_Controller{
	public $category;
	public function index($sub='pageMail',$page=0){
		
		$get = $this->uri->uri_to_assoc();
		
		$adminMenu= $this->load->module('adminMenu/index');
		
		$category= $this->load->module('category/admin');
		$category->listTableCategory();
		
		

		$setting =$this->load->Module('setting/admin');
		$this->template=$this->config->config['admin_template'];
		
		$subscribe=$this->load->module('subscribe/admin');
		$email=$subscribe->listmail($page);
		
		$this->load->helper('url');
		
		$data=array(
					'update_footer'=>'StarView 2013',
					'admin_title'=>'Newsletters Admin',
					'admin_menu'=>$adminMenu->generateAdminMenu(),
					'emailList'=>$email['emails'],
					'page'=>$email['page'],
					'listNewsletter'=>$this->model()->list_newslettre(),
					'tableCategoryBody'=>$category->category
				);
		
		
		$this->view(
				array('admin-header',
						'newsletter',
						'admin-footer'), 
				$data); // Display the page
	}
	public function archive($sub='pageMail',$page=0){
		self::edit(0,$sub,$page);
	}
	public function edit($id,$sub='pageMail',$page=0){
	
		$get = $this->uri->uri_to_assoc();
	
		$adminMenu= $this->load->module('adminMenu/index');
	
		$category= $this->load->module('category/admin');
		$category->listTableCategory();
	
	
	
		$setting =$this->load->Module('setting/admin');
		$this->template=$this->config->config['admin_template'];
	
		$subscribe=$this->load->module('subscribe/admin');
		$email=$subscribe->listmail($page);
	
		$this->load->helper('url');
	
		$data=array(
				'update_footer'=>'StarView 2013',
				'admin_title'=>'Newsletters Admin edit',
				'admin_menu'=>$adminMenu->generateAdminMenu(),
				'emailList'=>$email['emails'],
				'page'=>$email['page'],
				'listNewsletter'=>$this->model()->list_newslettre(),
				'newsletter'=>$this->model()->get_newslettre($id),
				'tableCategoryBody'=>$category->category
		);
	
	
		$this->view(
				array('admin-header',
						'newsletter-edit',
						'admin-footer'),
					$data); // Display the page
		}
	public function create() {
		$save=$this->input->post('save');
		$send=$this->input->post('send');
		$newsletter=new stdClass;
		$newsletter->fromname=$this->input->post('fromname');
		$newsletter->fromemail=$this->input->post('fromemail');
		$newsletter->subject=$this->input->post('subject');
		$newsletter->body=$this->input->post('body');
		$newsletter->date_create=strtotime('now');
		
		$this->model()->save($newsletter);
		if($send){
			self::send($newsletter);
		}

		$this->load->helper('url');
		$url=$this->config->config['base_url'].'newsletter/admin';
		redirect($url);
	}
	
	
	public function edit_news() {
		$save=$this->input->post('save');
		$send=$this->input->post('send');
		$newsletter=new stdClass;
		$newsletter->id=$this->input->post('id');
		$newsletter->fromname=$this->input->post('fromname');
		$newsletter->fromemail=$this->input->post('fromemail');
		$newsletter->subject=$this->input->post('subject');
		$newsletter->body=$this->input->post('body');
		$newsletter->date_create=strtotime('now');
		
		$this->model()->edit($newsletter);
		if($send){
			self::send($newsletter);
		}

		$this->load->helper('url');
		$url=$this->config->config['base_url'].'newsletter/admin';
		redirect($url);
	}
	
	
	private  function send($newsletter) {
		$query = $this->db->get_where('subscribe', array('nSubtype' => null), $limit, $offset);
		
		foreach ($query->result() as $row){
			$this->load->library('email');
			
			$this->email->from($newsletter->fromemail,$newsletter->fromname);
			$this->email->to( $row->sEmail);
			
			$this->email->subject($newsletter->subject);
			$this->email->message($newsletter->body);
			
			$this->email->send();
			$this->model()->sent( $row->sEmail);
		}
	}
}